<?php
include('rootlogin.php');



mysql_query("SET NAMES 'utf8'");

if(!isset($_POST['topic']) || !isset($_POST['year_1']) || !isset($_POST['month_1']) || !isset($_POST['day_1']) || 
							!isset($_POST['year_2']) || !isset($_POST['month_2']) || !isset($_POST['day_2'])){
			$flag=0;
}
else if($_POST['topic'] == '' || $_POST['year_1'] == '' || $_POST['month_1'] == '' || $_POST['day_1'] == '' || 
							$_POST['year_2'] == '' || $_POST['month_2'] == '' || $_POST['day_2'] == '' ||
		$_POST['topic'] == ' ' || $_POST['year_1'] == ' ' || $_POST['month_1'] == ' ' || $_POST['day_1'] == ' ' || 
							$_POST['year_2'] == ' ' || $_POST['month_2'] == ' ' || $_POST['day_2'] == ' ' || $_POST['class']==''){
			$flag=1;	
}
else{
	$class=$_POST['class'];
	$from=$_POST['year_1']."-".$_POST['month_1']."-".$_POST['day_1'];
	$to=$_POST['year_2']."-".$_POST['month_2']."-".$_POST['day_2'];
	$t = str_replace("'", "&#39;", $_POST['topic']);
	$t = str_replace('"', '&quot;', $t);
	$query = "INSERT INTO topic (name, from_date, to_date) VALUES ('".$t."', '".$from."', '".$to."')";
	$result = mysql_query($query,$user_con) or die('<font color="#C11B17">Error. Please go back to the previous page and try again.</font>');	
	$query = "SELECT topicid FROM topic WHERE name='".$_POST['topic']."' ORDER BY topicid DESC";
	$result = mysql_query($query,$user_con);
	$row = mysql_fetch_array($result);	
	$_SESSION['topicid']=$row['topicid'];
	for($i=0; $i<sizeof($class); $i++){
		$query = "INSERT INTO topic_class (topicid, classid) VALUES (".$_SESSION['topicid'].",".$class[$i].")";
		$result = mysql_query($query,$user_con) or die('<font color="#C11B17">Error. Please go back to the previous page and try again.</font>');	
	}
	//echo $_SESSION['topic'];
	header ("location: video_intro1.php");
}
?>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="framestyle.css" type="text/css" media="screen">
<style type="text/css">

</style>

</head>
<body>
<div id="wrapper">
<div id="title">Online Listening Comprehension Program</div>

<div id="main">

<form method = POST action="topic.php" >

<hr/>
<div id="title2">ADD A NEW TOPIC</div>
<hr/>
<br><br>
<?php
	if($flag==1){
		echo "<div id='error'>Please fill in all the column.</div><br>";
	}
?>
topic name: 
<input class="area" type="text" name="topic"><br><br>

<table width=80%>
<tr>
<td align="right" width=135px>duration: from </td>
<td align="left"> 
<input class="area year" type="text" name="year_1"> / 
<input class="area month_day" type="text" name="month_1"> /
<input class="area month_day" type="text" name="day_1"> &nbsp;<font size=4>example: 2010.10.31</font>

<tr>
<td align="right" width=135px>to 
<td align="left"><input class="area year" type="text" name="year_2"> /
<input class="area month_day" type="text" name="month_2"> /
<input class="area month_day" type="text" name="day_2">
</table>
<br>
classes that will use this topic:
<table>
<tr bgcolor="CCCCCC">
<td width=40px align="center"><input type='checkbox' onClick="javascript:select_all()">
<td width=60px align="center"><font color="FFFFFF" size=4>YEAR</font>
<td width=110px align="center"><font color="FFFFFF" size=4>SEMESTER</font>
<td width=250px align="center"><font color="FFFFFF" size=4>TEACHER</font>
<td width=250px align="center"><font color="FFFFFF" size=4>CLASS NAME</font>


<?php
$query="SELECT * FROM class WHERE valid=1 ORDER BY year DESC, semester";
$result = mysql_query($query,$user_con);
$i=0;
while($row[$i] = mysql_fetch_array($result)){
	if($row[$i]['valid']==0)
		$valid="expired";
	else
		$valid="valid";
	echo "<tr><td align='center'><div id='check_".$i."'><input type='checkbox' value=".$row[$i]['classid']." name='class[]'></div><td>".$row[$i]['year']."<td>".$row[$i]['semester']."<td>".$row[$i]['teacher']."<td>".$row[$i]['name'];	
	$i++;
}

?>

</table>
<script>

function select_all(){
<?php
//echo "var count=".$i.";";

for($i=0; $i<sizeof($row[$i]); $i++){
	echo "document.getElementById('check_".$i."').innerHTML='<input type=\"checkbox\" value=".$row[$i]['classid']." name=\"class[]\" checked >';";	
}
?>

}
</script>
<br>
<div class="btn_right"><input type="submit" name = "next" value = "next"></div>
</form>

</div>
</div>
</body>

